package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.Utils.SubmitValueVerification;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.HistoryList;
import com.jzg.jcptadmin.data.vo.ValuationDetailData;
import com.jzg.jcptadmin.viewinterface.HistoryActivityInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by wangyd on 16/1/7.
 */
public class HistoryPresenter extends BasePresenter<HistoryActivityInterface> {
    private final DataManager mDataManager;
    private Subscription mSubscription;
    private HistoryActivityInterface mView;

    public HistoryPresenter(DataManager dataManager) {
        mDataManager = dataManager;
    }

    @Override
    public void attachView(HistoryActivityInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void loadHistory() {
        Map<String, String> params = mView.getHistoryParams();
        if (AppContext.isNetWork)
            mSubscription = mDataManager.loadHistory(SubmitValueVerification.filterParams(params)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<HistoryList>() {
                @Override
                public void onCompleted() {
                    mView.dismissDialog();
                }

                @Override
                public void onError(Throwable e) {
                    if (e != null&&mView!=null) {

                        mView.showError(e.toString());
                    }
                }

                @Override
                public void onNext(HistoryList historyList) {
                    mView.showHistory(historyList);
                }
            });
    }

    public void loadEstimate() {
        Map<String, String> params = mView.getEstimateParams();
        if (!checkSubmitInfo(params)) return;
        mSubscription = mDataManager.LoadValuationDetailData(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<ValuationDetailData>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                if (e != null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(ValuationDetailData valuationDetailData) {
                if (valuationDetailData.getStatus() == Constant.SUCCESS)
                    mView.loadValuationDetail(valuationDetailData);
                else
                    mView.showError(valuationDetailData.getMsg());
            }
        });
    }


}
